// 时间
package time

import (
	"time"
)

// Time 获取当前时间戳
func Time() int64 {
	return time.Now().Unix()
}

// TimeToDate 时间戳格式化 timestamp=0 默认为当前时间
func TimeToDate(timestamp int64, timeLayout string) string {
	if timestamp == 0 {
		timestamp = Time()
	}
	if timeLayout == "" {
		timeLayout = "2006-01-02 15:04:05"
	}
	return time.Unix(timestamp, 0).Format(timeLayout)
}

// NewDateTime 创建一个时间Time
func NewDateTime(dateTime, timeLayout string) time.Time {
	if timeLayout == "" {
		timeLayout = "2006-01-02 15:04:05"
	}
	times, _ := time.ParseInLocation(timeLayout, dateTime, time.Local)
	return times
}

// StrToTime 字符串转时间戳
func StrToTime(datetime, timeLayout string) int64 {
	times := NewDateTime(datetime, timeLayout)
	return times.Unix()
}

// RfcToLocalDatetime rfc时间格式化成指定的格式
func RfcToLocalDatetime(rfcTime, layout string) string {
	if layout == "" {
		layout = "2006-01-02 15:04:05"
	}
	t, _ := time.Parse(time.RFC3339, rfcTime)
	return t.Format(layout)
}

// RfcToLocalTime rfc时间格式化成时间戳
func RfcToLocalTime(rfcTime, layout string) int64 {
	if layout == "" {
		layout = "2006-01-02 15:04:05"
	}
	t, _ := time.Parse(time.RFC3339, rfcTime)
	return t.Unix()
}
